<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <el-col :span="12">
        <div class="opration_wrap">
          <el-button
            type="primary"
            icon="fa fa-mail-forward"
            @click="returnList"
            >返回</el-button
          >
        </div>
      </el-col>
    </el-row>
    <div class="add_wrap">
      <div class="tit">
        <h2>基础信息</h2>
      </div>
      <el-form
        :model="addForm"
        ref="addForm"
        label-width="200px"
        :rules="roleRules"
      >
        <el-row>
          <el-form-item label="手机号码" prop="tel">
            <el-input
              v-model="addForm.tel"
              placeholder="请选输入手机号码"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="addForm.password"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="customerNickName">
            <el-input
              v-model="addForm.customerNickName"
              placeholder="请输入昵称"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select v-model="addForm.sex" placeholder="请选择性别">
              <el-option key="1" label="男" value="1"></el-option>
              <el-option key="2" label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="出生年月" prop="birthday">
            <el-date-picker
              v-model="addForm.birthday"
              type="date"
              placeholder="请选择日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="地区" prop="areaIds">
            <ProvinceCity
              :provinceCityValues="addForm.areaIds"
              @provinceCityChange="handleItemChange"
            ></ProvinceCity>
          </el-form-item>
          <el-form-item label="推荐人手机号码" prop="inviterLoginName">
            <el-input
              v-model="addForm.inviterLoginName"
              placeholder="请输入推荐人手机号码"
            ></el-input>
          </el-form-item>
          <el-form-item label="简介" prop="userIntroduce">
            <el-input
              type="textarea"
              v-model="addForm.userIntroduce"
              placeholder="请输入简介"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <el-button type="primary" @click="sureClick">保 存</el-button>
            <el-button @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as validators from "@/utils/validators";
import * as message from "@/constants/message";
import Crumbs from "@/components/Crumbs";
import ProvinceCity from "@/components/ProvinceCity";
export default {
  data() {
    return {
      addForm: {
        imageUrl: "",
        tel: "",
        password: "",
        customerNickName: "",
        areaIds: [],
        sex: "",
        birthday: "",
        inviterLoginName: "",
        userIntroduce: ""
      },
      roleRules: {
        imageUrl: [
          { required: true, message: "请选择头像", trigger: "change" }
        ],
        tel: [
          { validator: this.$Utils.elIsPhone, trigger: "blur", required: true }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        birthday: [
          { required: true, message: "请选择出生年月", trigger: "change" }
        ],
        inviterLoginName: [
          { validator: this.$Utils.elIsPhone, trigger: "blur", required: true }
        ],
        customerNickName: [
          { required: true, message: "请输入昵称", trigger: "blur" }
        ],
        areaIds: [
          { required: true, message: "请选择所在地区", trigger: "change" }
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }]
      }
    };
  },
  components: {
    Crumbs,
    ProvinceCity
  },
  methods: {
    handleItemChange(val) {
      this.addForm.areaIds = [val.provinceId, val.cityId];
    },
    handleFile(id, data) {
      const $target = document.getElementById(id);
      const _this = this;
      const file = $target.files[0];
      if (!this.beforeAvatarUpload(file)) {
        return;
      }
      this.$Get("/aliyun/sts/token").then(res => {
        let OSS = require("ali-oss");
        let client = new OSS({
          region: _this.$Config.oss.region,
          accessKeyId: res.data.accessKeyId,
          accessKeySecret: res.data.accessKeySecret,
          stsToken: res.data.securityToken,
          bucket: _this.$Config.oss.bucket
        });
        const fileName = file.name;
        const fileType = fileName.split("\.")[1];
        let fileTempName = new Date().getTime() + fileName.split("\.")[0];
        if (fileTempName.length > 5) {
          fileTempName = fileTempName.substring(0, 12);
        }
        const newName = fileTempName + "." + fileType;
        const storeAs = "web_images/Customer/" + newName;
        const resultUpload = "";
        client
          .put(storeAs, file, {})
          .then(function(r1) {
            let realUrl = r1.url.replace(
              "cactus-pro.oss-cn-hangzhou.aliyuncs.com",
              "image.vvhuimei.com"
            );
            _this.addForm[data] = realUrl;
          })
          .catch(function(err) {
            console.error(err);
          });
      });
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isBMP = file.type === "image/bmp";
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG && !isPNG && !isBMP) {
        this.$message.error("上传图片只能是 JPG , PNG , BMP 格式!");
      }

      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 5MB!");
      }
      return (isJPG || isPNG || isBMP) && isLt2M;
    },
    sureClick() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.$confirm(
            message.MSG_CONFIRM_SAVE,
            message.TITLE_PROMPT,
            {}
          ).then(() => {
            this.addData();
          });
        }
      });
    },
    addData() {
      let data = {
        imageUrl: this.addForm.imageUrl,
        tel: this.addForm.tel,
        password: this.addForm.password,
        customerNickName: this.addForm.customerNickName,
        areaIds: this.addForm.areaIds,
        sex: this.addForm.sex,
        birthday: this.addForm.birthday,
        inviterLoginName: this.addForm.inviterLoginName,
        userIntroduce: this.addForm.userIntroduce
      };
      console.log(data);
      this.submitData(data);
    },
    submitData(data) {
      this.$Post(this.$Config.api.customer, data)
        .then(() => {
          this.$notify.success({
            title: message.TITLE_SUCCESS,
            message: message.MSG_ACTION_SUCCESS
          });
          this.returnList();
        })
        .catch(error => {
          if (error.msg) {
            this.$notify.error({
              title: message.TITLE_ERROR,
              message: error.msg
            });
          }
        });
    },
    returnList() {
      this.$router.push({ name: "客户列表" });
    }
  }
};
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope></style>
